package com.shuqi.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.common.Config;
import com.shuqi.common.ConfigVersion;
import com.shuqi.common.MyTask;
import com.shuqi.common.SyncBookMarks.SyncResultLinstener;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.Util;
import com.shuqi.interfaces.EventTypeConstants;
import com.shuqi.model.bean.SyncBookMarksData;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.SyncBookMarksParser;

public class SyncBookMarksModel extends ModelBase implements HttpRequestListener {

    private Context context;
    private HttpRequest httpRequest;
    private String userId;
    private SyncResultLinstener resultLinstener;
    private String sendData="yes";//默认yes
    public SyncBookMarksModel(Context context, SyncResultLinstener resultLinstener) {
        this.context = context;
        this.resultLinstener = resultLinstener;
    }

    @Override
    public void action(int actionCode, Object object) {
        if (actionCode == EventTypeConstants.EVENT_SUCCESS) {
            resultLinstener.onResult(true, (SyncBookMarksData) object);
        } else {
            resultLinstener.onResult(false, null);
        }
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        long timestamp = System.currentTimeMillis() / 1000;
        String strTimestamp = timestamp + "";
        String key = "37e81a9d8f02596e1b895d07c171d5c9";
        StringBuilder md5_key_sb = new StringBuilder();
        md5_key_sb.append(userId);
        md5_key_sb.append(key);
        md5_key_sb.append(ConfigVersion.SN);
        md5_key_sb.append(Config.SOFT_ID);
        md5_key_sb.append(strTimestamp);

        String md5_key = Util.MD5(md5_key_sb.toString());
        // params.add(new BasicNameValuePair("act", "get"));
        params.add(new BasicNameValuePair("sn", ConfigVersion.SN));
        params.add(new BasicNameValuePair("user_id", userId));
        params.add(new BasicNameValuePair("timestamp", strTimestamp));// 时间戳
        params.add(new BasicNameValuePair("md5_key", md5_key)); // md5
        params.add(new BasicNameValuePair("appid", Config.SOFT_ID));
        return params;
    }
    
    
    @Override
    public String getRequestUrl() {
        Log4an.i("texttext", "Urls.getSyncBookMarkUrl(sendData)=" + Urls.getSyncBookMarkUrl(sendData));
        return Urls.getSyncBookMarkUrl(sendData);
    }

    private List<BasicNameValuePair> addBookMarksXML(List<BasicNameValuePair> params,
            String bookMarkXML) {
        if (params == null) {
            params = new ArrayList<BasicNameValuePair>();
        }
        params.add(new BasicNameValuePair("bookmarks", bookMarkXML));
        Log4an.i("texttext", "上传xml数据：" + bookMarkXML);
        return params;
    }

    @Override
    public void loadData(Object... objs) {
        userId = (String) objs[0];
        sendData=(String) objs[1];
        String bookMarkXML = (String) objs[2];
        Log4an.i("texttext", "上传sendData：" + sendData);
        httpRequest =
                new HttpRequest(context, UrlManager.COLUMN_DOMAIN_WRITE, getRequestUrl(),
                        addBookMarksXML(setParams(), bookMarkXML), this);
        httpRequest.setDataParseAdapter(new SyncBookMarksParser());
        MyTask.runInBackground(httpRequest, true);
    }

    @Override
    public void onDestroy() {
        httpRequest.abort();
    }

}
